import json
import os

EVENTS_FILE_PATH = "data/events.json"


def load_all_events_from_file():
    try:
        with open(EVENTS_FILE_PATH, "r") as f:
            return json.load(f)
    except FileNotFoundError:
        print("事件文件不存在，将创建一个新文件。")
        return []
    except json.JSONDecodeError:
        print("事件文件格式错误，将创建一个新文件。")
        return []

def save_event_to_file(new_event_dict):
    all_events = load_all_events_from_file()
    all_events.append(new_event_dict)
    save_all_events_to_file(all_events)

def save_all_events_to_file(all_events_list):
    os.makedirs(os.path.dirname(EVENTS_FILE_PATH), exist_ok=True)
    with open(EVENTS_FILE_PATH, "w") as f:
        json.dump(all_events_list, f, indent=4)